//
//  WJShowPictureVC.m
//  WJBSApp
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJShowPictureVC.h"
#import "DALabeledCircularProgressView.h"
#import "WJMainModel.h"
#import "SVProgressHUD.h"

@interface WJShowPictureVC ()

//  加载的圆形宽
@property (nonatomic, strong) DALabeledCircularProgressView *progressView;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) WJBaseImageView *imageView;

@property (nonatomic, strong) WJBaseButton *backButton;

@property (nonatomic, strong) WJBaseButton *saveButton;
// 评论的按钮
@property (nonatomic, strong) WJBaseButton *plButton;

@end

@implementation WJShowPictureVC

- (void)dealloc {
    NSLog(@"%s",__func__);
    [[SDImageCache sharedImageCache] removeImageForKey:_imageView.sd_imageURL.absoluteString fromDisk:NO];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createView];
}

- (void)createView {
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.scrollView.backgroundColor = [UIColor colorWithRed:16/255.0 green:16/255.0 blue:16/255.0 alpha:1];
    [self.view addSubview:self.scrollView];
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

    self.backButton = [WJBaseButton buttonWithTitle:@"" frame: CGRectMake(20, 40, 30, 30) target:self action:@selector(back) dayBgColor:LABLECOLOR dayTextColor:BSGrayColor nightBgColor:FORBLACKCOLOR nightTextColor:BSWhiteColor];
    [self.backButton setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:0];
    self.backButton.layer.cornerRadius = 15;
    self.backButton.layer.masksToBounds = YES;
    [self.view addSubview:self.backButton];
    
    self.saveButton = [WJBaseButton buttonWithTitle:@"保存" frame:CGRectMake(20, HEIGHT - 40, 50, 30) target:self action:@selector(save) dayBgColor:LABLECOLOR dayTextColor:BSGrayColor nightBgColor:FORBLACKCOLOR nightTextColor:BSWhiteColor];
    self.saveButton.layer.cornerRadius = 3;
    [self.view addSubview:self.saveButton];
    
    self.plButton = [WJBaseButton buttonWithTitle:nil frame:CGRectMake(WIDTH - 60, HEIGHT - 40, 50, 30) target:self action:@selector(backPLVC) dayBgColor:LABLECOLOR dayTextColor:BSGrayColor nightBgColor:FORBLACKCOLOR nightTextColor:BSWhiteColor];
    self.plButton.layer.cornerRadius = 3;
    [self.plButton setImage:[UIImage imageNamed:@"mainCellComment"] forState:UIControlStateNormal];
    [self.view addSubview:self.plButton];

    
    //添加图片
    self.imageView = [[WJBaseImageView alloc]init];
    self.imageView.userInteractionEnabled = YES;
    [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(back)]];
    [self.scrollView addSubview:self.imageView];
    
    CGFloat pictureH = self.model.image.height * WIDTH / self.model.image.width;
    self.imageView.frame = CGRectMake(0, 0, WIDTH, pictureH);
    self.scrollView.contentSize = CGSizeMake(0, pictureH);
    if (pictureH < HEIGHT) {
        self.imageView.center = self.view.center;
    }
    
    //下载图片
    NSString *url = @"";
    if (self.model.image.big && self.model.image.big.count > 0) {
       url = self.model.image.big[0];
    }
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:BSPlaceholder];
}

- (void)back {
     [self.navigationController popViewControllerAnimated:YES];
}

- (void)save {
    if (self.imageView.image == nil) {
        [SVProgressHUD showErrorWithStatus:@"图片并未下载完毕"];
        return;
    }
    
    /**************** 将图片写入相册 ****************/
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        [SVProgressHUD showErrorWithStatus:@"保存失败"];
    } else {
        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    }
}

#pragma marj - 返回评论页面
- (void)backPLVC
{
    
}
@end
